package com.hr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.mapper.ScheduleMapper;
import com.hr.pojo.Schedule;
import com.hr.service.ScheduleService;
import com.hr.utils.PageBean;
import com.hr.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author DSL
 * @create 2024-10-30 11:35
 */
@Service// 将当前类交给spring管理
public class ScheduleServiceImpl implements ScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Override
    public R page(int pageSize, int currentPage) {
//        分页
        PageHelper.startPage(pageSize, currentPage);
//        查询
        List<Schedule> schedulesList = scheduleMapper.selectAll();
//        PageBean: 封装分页数据
        PageInfo<Schedule> pageInfo = new PageInfo<>(schedulesList);
//        配置PageBean
//        pageInfo.getTotal() 总条数
//        pageInfo.getList() 当前数据
        PageBean<Schedule> pageBean = new PageBean<>(pageSize,currentPage,
                pageInfo.getTotal(),pageInfo.getList());
//        将分页参数放到提前准备好的R方法中
        R r=R.ok(pageBean);
//        分页查询数据
        System.out.println("成功");
        return r;
    }

    @Override
    public R deleteById(Integer id) {
        int  rows = scheduleMapper.deleteById(id);
//        删除判断，如果数据大于0则删除成功
        if (rows>0){
            return R.ok("200");// 删除成功
        }
        return R.fail("500");
    }
    @Override
    public R insert(Schedule schedule) {
        int rows = scheduleMapper.insert(schedule);
        if (rows>0){
            return R.ok("200");// 添加成功
        }
        return R.fail("500");
    }
    @Override
    public R update(Schedule schedule) {
        if (schedule.getId()==null){
            return R.fail("核心数据为空，不能修改");
        }
        int rows = scheduleMapper.update(schedule);
        if (rows>0){
            return R.ok("200");// 修改成功
        }

        return R.fail("500");
    }
}
